﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Comm.TransactSql;
using System.Data;
using VK.Casemeet.Employees;

namespace VK.Casemeet.Web.Admin.Employees
{
    public partial class EmployeeList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="管理员列表",Url="#"}
            };

            if (!IsPostBack)
            {
                EmployeeHelper.BindRoleDDL(ddlRoleID, OptionType.Search);
                LoadData(false);
            }
        }

        protected void LoadData(bool isCondition)
        {
            IList<Condition> conditionlst = new List<Condition>();
            if (isCondition)
            {
                conditionlst.Add(new Condition { TableName = "Employee", FieldName = "Account", ConditionType = ConditionType.TextLIKE, ConditionValue = txtAccount.Text.Trim(), DbType = DbType.String });
                conditionlst.Add(new Condition { TableName = "Employee", FieldName = "RoleID", ConditionType = ConditionType.NumeralEqual, ConditionValue = ddlRoleID.SelectedValue, DbType = DbType.Int32 });
            }
            aspNetPager.RecordCount = EmployeeBLL.GetEmployeeCount(conditionlst);
            rptEmployee.DataSource = EmployeeBLL.GetEmployeeList(aspNetPager.StartRecordIndex, aspNetPager.EndRecordIndex, conditionlst, string.Empty, SortMode.Desc);
            rptEmployee.DataBind();
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            LoadData(true);
        }

        protected void aspNetPager_PageChanged(object sender, EventArgs e)
        {
            LoadData(true);
        }

        protected void rptEmployee_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                switch (e.CommandName)
                {
                    case "Delete":
                        if (EmployeeBLL.DeleteEmployee(int.Parse(e.CommandArgument.ToString())) > 0)
                        {
                            Message.Msg(this.Page, CS.MsgDelSuccess, MessageType.Success);
                        }
                        else
                        {
                            Message.Msg(this.Page, CS.MsgDelFailure, MessageType.Failure);
                        }
                        break;
                    case "Reset":
                        if (EmployeeBLL.ResetPass(int.Parse(e.CommandArgument.ToString()), DES.Encrypt("000000", CS.AdminPassKey)) > 0)
                        {
                            Message.Msg(this.Page, CS.MsgResetSuccess, MessageType.Success);
                        }
                        else
                        {
                            Message.Msg(this.Page, CS.MsgResetFailure, MessageType.Failure);
                        }
                        break;
                }
                LoadData(true);
            }
        }
    }
}